
import { App, Adapter, Shaders, BitMath, IO } from "../lib.letusbit/lib"
import { Renderer, Render_Texture, ShaderResources } from "../lib.letusbit/renderer/adapter";

export class myloop implements App.IUserCode
{
    batcher?: Renderer.SpriteBatcher;
    bufrect: Renderer.SpritePoint[] = [
        { x: 0, y: 0, z: 0, r: 1, g: 1, b: 1, a: 1, u: 0, v: 1 },
        { x: 256, y: 0, z: 0, r: 1, g: 1, b: 1, a: 1, u: 1, v: 1 },
        { x: 0, y: 32, z: 0, r: 1, g: 1, b: 1, a: 1, u: 0, v: 0 },
        { x: 256, y: 32, z: 0, r: 1, g: 1, b: 1, a: 1, u: 1, v: 0 },
    ];
    addTexture(rect: BitMath.Rect, mat: Renderer.Material): void
    {
        this.bufrect[0].x = rect.x;
        this.bufrect[0].y = rect.y;
        this.bufrect[1].x = rect.x + rect.w;
        this.bufrect[1].y = rect.y;
        this.bufrect[2].x = rect.x;
        this.bufrect[2].y = rect.y + rect.h;
        this.bufrect[3].x = rect.x + rect.w;
        this.bufrect[3].y = rect.y + rect.h;
        this.batcher?.SetMat(mat);
        this.batcher?.addRect(this.bufrect);
        //this.batcher?.Render();
    }


    mat: Renderer.Material = new Renderer.Material();
    matp1: Renderer.Material = new Renderer.Material();
    matp2: Renderer.Material = new Renderer.Material();
    matp3: Renderer.Material = new Renderer.Material();
    matp4: Renderer.Material = new Renderer.Material();
    matp5: Renderer.Material = new Renderer.Material();
    mat3: Renderer.Material = new Renderer.Material();
    matlutquick: Renderer.Material = new Renderer.Material();
    matlutslow: Renderer.Material = new Renderer.Material();

    matwithlut: Renderer.Material = new Renderer.Material();
    mat2withlut: Renderer.Material = new Renderer.Material();
    async OnInit(app: App.IFramework): Promise<void>
    {

        var ext = app.render.webgl.getExtension('OES_texture_half_float');
        console.log("ext=" + ext);
        this.batcher = app.render.CreateBatcher();

        this.mat.shader = Shaders.GetShaderProgram("default");
        this.mat3.shader = Shaders.GetShaderProgram("default");
        let tex = new Render_Texture.Texture(app.render.webgl, Render_Texture.TextureFormat.RGBAF16);
        let tex3 = new Render_Texture.Texture(app.render.webgl, Render_Texture.TextureFormat.RGBA);
        let img = new Image();
        img.src = "resources/cj.jpg";
        img.onload = () =>
        {
            tex.loadimg(img, false, false);
            tex3.loadimg(img, false, false);
        }
        this.mat.texMain = tex;
        this.mat3.texMain = tex3;

        this.matp1.shader = Shaders.GetShaderProgram("palette");

        {//调色板 原味
            let tex2 = new Render_Texture.Texture(app.render.webgl, Render_Texture.TextureFormat.GRAY);
            this.matp1.texMain = tex2;
            let data = new Uint8Array(256);
            for (var i = 0; i < 256; i++)
                data[i] = i;
            tex2.loadimgFromData(data, 16, 16, false, false);
        }
        this.matp2.shader = Shaders.GetShaderProgram("lightmap");
        {//lightmap src
            let tex2 = new Render_Texture.Texture(app.render.webgl, Render_Texture.TextureFormat.RGBA);
            this.matp2.texMain = tex2;
            let img2 = new Image();
            img2.src = "resources/Lightmap-0_comp_light.png";
            img2.onload = () =>
            {
                tex2.loadimg(img2, false, false);
            }
        }
        this.matp3.shader = Shaders.GetShaderProgram("default_noa");
        {
            let tex2 = new Render_Texture.Texture(app.render.webgl, Render_Texture.TextureFormat.RGBA);
            this.matp3.texMain = tex2;
            let img2 = new Image();
            img2.src = "resources/Lightmap-0_comp_light.png_a.png";
            img2.onload = () =>
            {
                tex2.loadimg(img2, false, false);
            }
        }
        //raw
        this.matp4.shader = Shaders.GetShaderProgram("default_noa");
        {
            let tex2 = new Render_Texture.Texture(app.render.webgl, Render_Texture.TextureFormat.RGBAF16);
            this.matp4.texMain = tex2;
            var b = await fetch("resources/Lightmap-0_comp_light.png.raw");
            var reader = new IO.binReader(await b.arrayBuffer());
            var width = reader.readUnsignedShort();
            var height = reader.readUnsignedShort();
            console.log("w=" + width + ",h=" + height);
            var data = new ArrayBuffer(width * height * 8);
            var bts = new Uint8Array(data);
            var f16 = new Uint16Array(data);
            reader.readBytes(bts, 0, bts.length);
            // //有点奇怪，不太像rgba排列
            for (let i = 0; i < width * height; i++)
            {
                // //     f16[i * 4 + 0] = 0x3c00;
                //   f16[i*4+1] = 0;
                // //     f16[i*4+2] = 0;
                // //     f16[i * 4 + 3] = 0x3c00;
            }
            tex2.loadimgFromData(f16, width, height, false, false);
        }

        this.matp5.shader = Shaders.GetShaderProgram("default_noa");
        {
            let tex2 = new Render_Texture.Texture(app.render.webgl, Render_Texture.TextureFormat.RGBAF32);
            this.matp5.texMain = tex2;
            var b = await fetch("resources/Lightmap-0_comp_light.png.raw32");
            var reader = new IO.binReader(await b.arrayBuffer());
            var width = reader.readUnsignedShort();
            var height = reader.readUnsignedShort();
            console.log("w=" + width + ",h=" + height);
            var data = new ArrayBuffer(width * height * 16);
            var bts = new Uint8Array(data);
            var f32 = new Float32Array(data);
            reader.readBytes(bts, 0, bts.length);
            tex2.loadimgFromData(f32, width, height, false, false);
        }
        let _pal = BitMath.Palette.CreateDefault();
        let _buf = new Uint8Array(65536);
        //show worldtexlut
        this.matlutquick.shader = Shaders.GetShaderProgram("palette");
        this.matlutquick.texMain = app.render.worldTexLUT;
        Renderer.RenderWebgl.ParseLUTLABQuick_Worker(_pal, _buf, app.render.worldTexLUT);

        //show slow lut
        this.matlutslow.shader = Shaders.GetShaderProgram("palette");
        let texlutslow = new Render_Texture.Texture(app.render.webgl, Render_Texture.TextureFormat.GRAY);
        this.matlutslow.texMain = texlutslow;
        //這玩意真的slow，所以最好還是離綫處理，算完存下來
        Renderer.RenderWebgl.ParseLUTLABSlow_Worker(_pal, _buf, texlutslow);

        this.matwithlut.shader = Shaders.GetShaderProgram("lut_palette");
        this.matwithlut.texMain = tex;

        this.mat2withlut.shader = Shaders.GetShaderProgram("lutcustom_palette");
        this.mat2withlut.texMain = tex;
        this.mat2withlut.uniforms["texlutcustom"] = texlutslow;
    }
    OnUpdate(app: App.IFramework): void
    {


        app.render.Begin();

        this.addTexture(new BitMath.Rect(0, 0, 256, 256), this.mat);
        this.addTexture(new BitMath.Rect(256, 0, 256, 256), this.mat);
        this.addTexture(new BitMath.Rect(512, 0, 256, 256), this.mat3);
        this.addTexture(new BitMath.Rect(32, 32, 128, 128), this.matp1);
        this.addTexture(new BitMath.Rect(140, 32, 256, 256), this.matp2);
        this.addTexture(new BitMath.Rect(400, 32, 256, 256), this.matp3);
        this.addTexture(new BitMath.Rect(660, 32, 256, 256), this.matp4);
        this.addTexture(new BitMath.Rect(900, 32, 256, 256), this.matp5);

        this.addTexture(new BitMath.Rect(0, 296, 1024, 64), this.matlutquick);
        this.addTexture(new BitMath.Rect(0, 364, 1024, 64), this.matlutslow);

        this.addTexture(new BitMath.Rect(64, 324, 256, 256), this.matwithlut);
        this.addTexture(new BitMath.Rect(340, 324, 256, 256), this.mat2withlut);
        this.batcher?.Render();
        app.render.End();
    }
    OnResize(app: App.IFramework): void
    {

    }
}
